package com.fsj.miniapp.service.impl;

import com.fsj.miniapp.entity.Notice;
import com.fsj.miniapp.service.NoticeService;
import com.fsj.miniapp.utils.DateTimeUtils;
import com.fsj.miniapp.vo.NoticeVo;
import com.google.common.collect.Lists;
import com.stip.mybatis.generator.plugin.BaseExample;
import com.stip.mybatis.generator.plugin.BaseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class NoticeServiceImpl extends BaseService<Notice, BaseExample, Long> implements NoticeService {
    @Override
    public List<NoticeVo> listFor(Long merchantId,int pageSize) {

        BaseExample example = new BaseExample();

        example.createCriteria().addCriterion("merchant_id=", merchantId);

        example.setFromRowNum(1);
        example.setToRowNum(pageSize);

        example.setOrderByClause(" created_date desc");

        List<Notice> notices = selectByExample(example);

        if (!CollectionUtils.isEmpty(notices)) {
            return notices.stream().map(f -> {
                NoticeVo vo = new NoticeVo();
                BeanUtils.copyProperties(f, vo);
                vo.setDateAdd(DateTimeUtils.formatDateTime(f.getCreatedDate()));
                return vo;
            }).collect(Collectors.toList());
        }


        return Lists.newArrayList();
    }
}